<template>
  <div class="order_container">
    <div class="adress">
      <div class="title">收获地址</div>
      <div class="boxCheck">
        <a-radio-group v-model:value="CheckWay">
          <a-radio value="0">无需邮寄</a-radio>
          <a-radio value="1" disabled>快递邮寄</a-radio>
        </a-radio-group>
      </div>
      <span style="margin-left: 15px; margin-top: 7px; color: #bbb; font-size: 12px;">无需填写地址，付款后卖家将提供相关交易凭证</span>
      <div class="cancle" @click="cancleHandle">×</div>
    </div>
    <div class="order">
      <div class="title">订单信息</div>
      <div class="order_info">
        <div class="img_box">
          <img :src="goodDetailStore.goodDetail.images[0]" alt="">
        </div>
        <div class="info">
          <div class="name">{{ goodDetailStore.goodDetail.title }}</div>
          <div class="price">¥{{ goodDetailStore.goodDetail.price }}</div>
        </div>
      </div>
      <div class="counter">
        <div class="count_tit">购买数量</div>
        <a-input-number id="inputNumber" size="small" v-model:value="counter" :min="1"
          :max="goodDetailStore.goodDetail.putawayNumber" />
      </div>
    </div>
    <div class="price_wrapper">
      <div class="title">价格明细</div>
      <div class="price_item">
        <span class="tip">商品总价</span>
        <span class="price_tip">¥{{ sumPrice }}</span>
      </div>
      <div class="price_item">
        <span class="tip">运费</span>
        <span class="price_tip">¥0</span>
      </div>
    </div>
    <div class="btn">
      <div class="price_sum">
        <span style="font-size: 14px;">合计:</span>
        <span style="font-size: 18px; font-weight: bold;"> <span style="font-size: 15px;">¥</span>{{ sumPrice }}</span>
      </div>
      <div class="btn_click">
        <button @click="orderClick">确认订单</button>
      </div>
    </div>
  </div>
  <div class="pay_container">
    <a-modal v-model:open="payShow" title="支付" @ok="handlePay">
      是否确定跳转第三方支付
    </a-modal>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useOrderStore } from '@/stores/module/order/order';
import { orderGood } from '@/service/module/goods/goods';
import { useGoodDetailStore } from '@/stores/module/goods/good';
import { payGood } from '@/service/module/goods/goods';
const goodDetailStore = useGoodDetailStore()
const orderStore = useOrderStore()
const sumPrice = computed(() => {
  return Number(goodDetailStore.goodDetail.price) * counter.value
})



const CheckWay = ref('0')

const counter = ref<number>(1);

const payShow = ref(false)

const cancleHandle = () => {
  orderStore.isShow = false;
};

const payId = ref('')
const orderClick = async () => {
  const res = await orderGood(goodDetailStore.goodDetail.id, counter.value)
  payId.value = res.data
  payShow.value = true
}
const handlePay = async () => {
  //接收返回的form表单
  const res = await payGood(payId.value)
  handleAlipayResponse(res)
}

//手动创建dom,插入表单,执行脚本
function handleAlipayResponse(htmlString) {
  const tempDiv = document.createElement('div');
  tempDiv.innerHTML = htmlString;

  // 提取表单
  const form = tempDiv.querySelector('form[name="punchout_form"]');

  if (form) {
    // 动态添加到DOM
    document.body.appendChild(form);

    // 手动触发提交（替代原script）
    (form as HTMLFormElement).submit();

    // 提交后移除表单（可选）
    setTimeout(() => document.body.removeChild(form), 1000);
  } else {
    console.error('未找到支付表单');
  }
}

</script>

<style lang="less" scoped>
.order_container {
  position: fixed;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 60%;
  height: 520px;
  min-width: 540px;
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #ffffff;
  border-radius: 18px;

  .title {
    padding: 15px;
    font-size: 17px;
    font-weight: bold;
    color: #333333;
  }

  .adress {
    width: 100%;
    height: 106px;
    display: flex;
    flex-direction: column;
    border-bottom: 1px dashed #e8e8e8;
    position: relative;

    .cancle {
      position: absolute;
      top: 5px;
      right: 10px;
      font-size: 24px;
      color: #999999;
      cursor: pointer;
    }

    .boxCheck {
      padding: 0 15px;
    }
  }

  .order {
    width: 100%;
    height: 226px;
    display: flex;
    flex-direction: column;
    border-bottom: 1px dashed #e8e8e8;

    .order_info {
      padding: 5px;
      display: flex;

      .img_box {
        margin-left: 10px;
        width: 100px;
        height: 100px;
        margin-right: 22px;

        img {
          width: 100px;
          height: 100px;
          object-fit: cover;
          object-position: center;
          border-radius: 10px;
          border-radius: 8px;
        }
      }

      .info {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        width: 100%;
        height: 100%;

        .name {
          font-size: 15px;
          color: #242424;
          margin-bottom: 5px;
          width: 480px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        .price {
          font-size: 17px;
          color: rgb(255, 0, 0);
          font-weight: bold;
        }
      }


    }

    .counter {
      height: 26px;
      display: flex;
      padding: 15px;
      line-height: 26px;

      .count_tit {
        font-size: 16px;
        color: #242424;
        font-weight: bold;
        margin-right: 12px;
      }
    }
  }

  .price_wrapper {
    width: 100%;
    height: 100px;
    display: flex;
    flex-direction: column;
    border-bottom: 1px dashed #e8e8e8;


    .price_item {
      padding: 3px 15px;
      display: flex;
      justify-content: space-between;

      .tip {
        color: #333333;
        font-weight: bold;
      }

      .price_tip {
        font-weight: bold;
      }
    }
  }

  .btn {
    padding: 15px;
    width: 96%;
    height: 48px;

    display: flex;
    justify-content: space-between;

    .price_sum {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
    }

    button {
      width: 118px;
      height: 42px;
      border-radius: 18px;
      outline: none;
      border: none;
      background-color: #FF4200;
      color: #ffffff;
      cursor: pointer;
    }
  }
}
</style>
